package com.rdc.rdcmusicapp.mainActivity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.rdc.rdcmusicapp.R;
import com.rdc.rdcmusicapp.bean.LocalMusicBean;

import java.util.List;

/**
 * [主页面RecyclerView的适配器]
 *
 * @author Suzy.Mo
 * @createTime 2021/4/24 14:25
 */

public class LocalMusicAdapter extends RecyclerView.Adapter<LocalMusicAdapter.LocalMusicViewHolder> {
    Context mContext;
    List<LocalMusicBean> mDatas;

    OnItemClickListener onItemClickListener;
/**
 * @description 传递监听接口的函数
 * @param 
 * @return 
 * @author Suzy.Mo
 * @time 2021/4/24
 */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    //  item点击事件的接口
    public interface OnItemClickListener {
        void OnItemClick(View view, int position);
    }

    public LocalMusicAdapter(Context mContext, List<LocalMusicBean> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
    }

    @NonNull
    @Override
    public LocalMusicViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.music_recycle_item, parent, false);
        LocalMusicViewHolder holder = new LocalMusicViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull LocalMusicViewHolder holder, final int position) {
        LocalMusicBean musicBean = mDatas.get(position);
        holder.idTv.setText(musicBean.getMid());
        holder.songTv.setText(musicBean.getmSong());
        holder.singerTv.setText(musicBean.getmSinger());
        holder.albumTv.setText(musicBean.getmAlbum());
        holder.timeTv.setText(musicBean.getmDuration());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.OnItemClick(v, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class LocalMusicViewHolder extends RecyclerView.ViewHolder {
        TextView idTv, songTv, singerTv, albumTv, timeTv;

        public LocalMusicViewHolder(View itemView) {
            super(itemView);
            idTv = itemView.findViewById(R.id.item_local_music_num);
            songTv = itemView.findViewById(R.id.item_local_music_song);
            singerTv = itemView.findViewById(R.id.item_local_music_singer);
            albumTv = itemView.findViewById(R.id.item_local_music_album);
            timeTv = itemView.findViewById(R.id.item_local_music_durtion);
        }
    }
}
